import { Util } from './Utils';
import { Color } from './Color';
import axios from 'axios';

/**
 * v1.0.5
 * VGE入口
 */
export default class VGE {
	static get tools() {
		return this.Viewer?.tools;
	}

	static get toolManager() {
		return this.Viewer?.toolManager;
	}

	static get Viewer() {
		return this._viewer;
	}

	static set Viewer(viewer) {
		viewer.engineType = this._sceneTo;
		this._viewer = viewer;
	}

	static get engineType() {
		return this._sceneTo;
	}

	static get Scene() {
		return this._viewer;
	}

	static get camera() {
		return this.Viewer?.camera;
	}

	static get color() {
		return new Color();
	}

	static get util() {
		return new Util();
	}

	static get config() {
		return this._config;
	}

	static get request() {
		return axios.create({
			timeout: 1000 * 10
		});
	}

	/**
	 * 初始化配置颜色、工具联动等
	 * @param _config {object}
	 * @returns {Promise<any>}
	 */
	static async initConfig(_config) {
		if (_config.toString() !== '[object Object]') return;
		return Object.assign(this._config, _config);
	}

	/**
	 * 打开场景
	 * @returns {Promise<*>}
	 * @param sceneData {object}
	 * @param config {object}
	 * @param callback {function}
	 */
	static async open(sceneData = {}, config = {}, callback) {
		try {
			const { sceneTo, ...args } = sceneData;

			this._sceneFrom = this._sceneTo;
			this._sceneTo = sceneTo;
			this._container = args.container;

			await this.initConfig(config);

			this.config.single && (await this.close());

			this.Viewer = await this.getEngineViewer(sceneTo.toUpperCase());

			if (!this._animationRequested) {
				this._animationRequested = true;
				this.requestAnimationFrame();
			}

			return this.Viewer?.open(args, callback);
		} catch (e) {
			console.error(e);
		}
	}

	/**
	 * 关闭场景
	 * @returns {Promise<void>}
	 */
	static async close() {
		await this.Viewer?.close();
	}

	/**
	 * 角度转为弧度
	 * @param val
	 * @returns {number}
	 */
	static toRadians(val) {
		return (val * Math.PI) / 180;
	}

	/**
	 * 弧度转为角度
	 * @param val
	 * @returns {number}
	 */
	static toDegrees(val) {
		return (val * 180) / Math.PI;
	}

	/**
	 * 全屏展示
	 * @param flag
	 * @param parent
	 */
	static fullScreen(flag, parent = false) {
		if (!flag) {
			// 退出全屏
			document.exitFullscreen && document.exitFullscreen();
		} else {
			// 进入全屏
			this.util.findOrCreateDom(this._container, true, false).then((res) => {
				const dom = parent ? res.parentNode : res;
				dom.requestFullscreen();
			});
		}
	}

	/**
	 * 获取引擎主对象
	 * @returns {Promise<*>}
	 * @param engineType
	 */
	static async getEngineViewer(engineType) {
		switch (engineType) {
			// case 'SWANMAP':
			//   return (await import('./MAP/MAP-SwanMap/SwanApp')).default
			case 'MAPBOX':
				return (await import('./MAP/MAP-Mapbox/MapApp')).default;
			case 'AMAP':
				return (await import('./MAP/MAP-AMap/AMapApp')).default;
			case 'TMAP':
				return (await import('./MAP/MAP-TMap/TMapApp')).default;
			case 'BMAP':
				return (await import('./MAP/MAP-BMap/BMapApp')).default;

			// case 'ARCGIS':
			// 	return (await import('./GIS/GIS-Arcgis/ArcgisApp')).default;
			case 'SUPERMAP':
				return (await import('./GIS/GIS-SuperMap/CesiumApp')).default;
			// case 'CGE':
			//   return (await import('./GIS/GIS-CGE/CGEApp')).default
			// case 'DTS':
			// 	return (await import('./VIZ/VIZ-DTS/AirCityApp')).default;
			// case 'CBE':
			//   return (await import('./BIM/BIM-CyberTwin/CBEApp')).default
			// case 'MODELO':
			//   return (await import('./BIM/BIM-Modelo/ModeloApp')).default
			case 'BIMBASE':
				return (await import('./BIM/BIM-BIMBase/BIMBaseApp')).default;
		}
	}

	static requestAnimationFrame() {
		let frame = 0;
		let lastTime = performance.now();
		const loop = (_) => {
			frame++;
			const now = performance.now();
			if (now > 1000 + lastTime) {
				this._fps = Math.round((frame * 1000) / (now - lastTime));
				frame = 0;
				lastTime = now;
			}
			requestAnimationFrame(loop);
		};
		requestAnimationFrame(loop);
	}
}
VGE._viewer = undefined;
VGE._sceneTo = undefined;
VGE._sceneFrom = undefined;
VGE._container = undefined;
VGE._config = {
	toolRelation: new Map(),
	backgroundColor: 'rgba(255,255,255,1)',
	primaryColor: 'rgba(6,206,174,1)',
	secondaryColor: 'rgba(11,223,141,1)',
	color: 'rgba(51,51,51,1)',
	t0tk: '2f10c0920b3c30cff7e93b4a0330d3e0',
	maxUndoSteps: 5,
	single: true
};
